package com.codeoss.framework.workflow.parser;

import org.springframework.stereotype.Service;

import com.codeoss.framework.expression.Expression;
import com.codeoss.framework.expression.ExpressionUtil;
import com.codeoss.framework.util.StringUtils;
import com.codeoss.framework.workflow.def.TaskNodeDef;
import com.codeoss.framework.workflow.def.WorkflowElementDef;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

@Service
public class TaskNodeParser  implements IWorkflowElementParser{

	@Override
	public WorkflowElementDef parse(IWorkflowElementParseContext workflowElementParseContext, ObjectNode objectNode) {
		// TODO Auto-generated method stub
		TaskNodeDef taskNode=new TaskNodeDef();
		JsonNode jsonNode=objectNode.get("name");
		if(jsonNode!=null) {
			taskNode.setName(jsonNode.asText());
		}
		jsonNode=objectNode.get("id");
		if(jsonNode!=null) {
			taskNode.setId(jsonNode.asText());
		}
		
		jsonNode=objectNode.get("startEventExpression");
		if(jsonNode!=null) {
			String startEventExpression=jsonNode.asText();
			if(!StringUtils.isEmpty(startEventExpression) && !"null".equals(startEventExpression)) {
				Expression expression=ExpressionUtil.convertToExpression(startEventExpression, Expression.EXPRESSION_TYPE_JAVASCRIPT);
				taskNode.setStartEventExpression(expression);
			}
		}
		
		jsonNode=objectNode.get("endEventExpression");
		if(jsonNode!=null) {
			String endEventExpression=jsonNode.asText();
			if(!StringUtils.isEmpty(endEventExpression) && !"null".equals(endEventExpression)) {
				Expression expression=ExpressionUtil.convertToExpression(endEventExpression, Expression.EXPRESSION_TYPE_JAVASCRIPT);
				taskNode.setEndEventExpression(expression);
			}
		}
		
		jsonNode=objectNode.get("userAssignment");
		if(jsonNode!=null) {
			String userAssignment=jsonNode.asText();
			if(!StringUtils.isEmpty(userAssignment) && !"null".equals(userAssignment)) {
				taskNode.setUserAssignment(userAssignment);
			}
		}
		
		jsonNode=objectNode.get("roleAssignment");
		if(jsonNode!=null) {
			String roleAssignment=jsonNode.asText();
			if(!StringUtils.isEmpty(roleAssignment) && !"null".equals(roleAssignment)) {
				taskNode.setRoleAssignment(roleAssignment);
			}
		}
		
		jsonNode=objectNode.get("assignmentExpression");
		if(jsonNode!=null) {
			String assignmentExpression=jsonNode.asText();
			if(!StringUtils.isEmpty(assignmentExpression) && !"null".equals(assignmentExpression)) {
				Expression expression=ExpressionUtil.convertToExpression(assignmentExpression, Expression.EXPRESSION_TYPE_JAVASCRIPT);
				taskNode.setAssignmentExpression(expression);
			}
		}
		
		jsonNode=objectNode.get("sequence");
		if(jsonNode!=null) {
			boolean sequence=jsonNode.asBoolean();
			taskNode.setSequence(sequence);
		}
		
		jsonNode=objectNode.get("completeRule");
		if(jsonNode!=null) {
			String completeRule=jsonNode.asText();
			if(!StringUtils.isEmpty(completeRule) && !"null".equals(completeRule)) {
				taskNode.setCompleteRule(completeRule);
			}
		}
		
		jsonNode=objectNode.get("count");
		if(jsonNode!=null) {
			int count=jsonNode.asInt();
			taskNode.setCount(count);
		}
		
		jsonNode=objectNode.get("percent");
		if(jsonNode!=null) {
			int percent=jsonNode.asInt();
			taskNode.setPercent(percent);
		}
		
		jsonNode=objectNode.get("canBack");
		if(jsonNode!=null) {
			boolean canBack=jsonNode.asBoolean();
			taskNode.setCanBack(canBack);
		}
		
		jsonNode=objectNode.get("canJump");
		if(jsonNode!=null) {
			boolean canJump=jsonNode.asBoolean();
			taskNode.setCanJump(canJump);
		}
		
		jsonNode=objectNode.get("canAddTask");
		if(jsonNode!=null) {
			boolean canAddTask=jsonNode.asBoolean();
			taskNode.setCanAddTask(canAddTask);
		}
		
		jsonNode=objectNode.get("canSubtractTask");
		if(jsonNode!=null) {
			boolean canSubtractTask=jsonNode.asBoolean();
			taskNode.setCanSubtractTask(canSubtractTask);
		}
		
		jsonNode=objectNode.get("canFetch");
		if(jsonNode!=null) {
			boolean canFetch=jsonNode.asBoolean();
			taskNode.setCanFetch(canFetch);
		}
		
		jsonNode=objectNode.get("canUserInput");
		if(jsonNode!=null) {
			boolean canUserInput=jsonNode.asBoolean();
			taskNode.setCanUserInput(canUserInput);
		}
		return taskNode;
	}

}
